/*
 * zfile.c
 *
 *  Created on: 24 avr. 2012
 *      Author: Florian.B
 */

#include "zfile.h"
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>

typedef struct {
    ZFile *zFile;
    GtkTreeIter *iter;
    int *sockPair;
    double s;
}ZFileProgress;

gboolean gIdleProgress_ZFile(void *arg)
{
  ZFileProgress *zFile = (ZFileProgress *)arg;
  if(zFile != NULL)
    {
      gtk_tree_store_set(zFile->zFile->store, zFile->iter, DOWN_COLUMN_FIC, (int)zFile->s, -1);
  
      if(zFile->s == 100)
	{
	  gtk_tree_iter_free(zFile->iter);
	  free(zFile);
	  zFile = NULL;
	  return FALSE;
	}  
    }
  return TRUE;
}

void *threadProgress_ZFile(void *arg)
{
  ZFileProgress *zFile = (ZFileProgress *)arg;
  double s;
  
  g_idle_add(gIdleProgress_ZFile, zFile);
 
  while(recv(zFile->sockPair[0], &s, sizeof(double), 0) > -1)
    {
       zFile->s = s;
    }
  
  return NULL;
}

void treeSelectionActivated_ZFile(GtkTreeView *tree_view, GtkTreePath *path,
	GtkTreeViewColumn *column, gpointer data)
    {
    GtkTreeIter iter;
    GtkTreeModel *model = NULL;
    gboolean b;
    ZFile *zFile = data;
    if (path)
	{
	char *file;
	Client *client;
	gtk_tree_model_get_iter(GTK_TREE_MODEL(zFile->store), &iter, path);

	gtk_tree_model_get(GTK_TREE_MODEL(zFile->store), &iter,
		NAME_COLUMN_FIC, &file, DOWNLOAD_COLUMN_FIC, &b,
		CLIENT_COLUMN_FIC, &client, -1);
	if (b)
	    {

	    }

	else
	    {
	    GtkWidget *dialog;

	    dialog = gtk_file_chooser_dialog_new("Enregistrement", NULL,
		    GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
		    GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
		    NULL);
	    gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(
		    dialog), TRUE);
	    gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), file);
	    if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
		{
		char *fileSave;
		int *socketPair;
		ZFileProgress *progress;
		pthread_t thread;
		socketPair = (int *) malloc (2*sizeof(int));
		if (socketpair(AF_UNIX, SOCK_STREAM, 0, socketPair)
			< 0)
		    {
		    GtkWidget * dialog;
		    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
			    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "La progression ne pourra pas être affichée."
			    );
		    gtk_dialog_run(GTK_DIALOG(dialog));
		    gtk_widget_destroy(dialog);
		    }

		gtk_tree_store_set(zFile->store, &iter, DOWNLOAD_COLUMN_FIC,
			!b, -1);
		fileSave = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(
			dialog));
		getFile_Client(client, file, fileSave, socketPair);
		progress = (ZFileProgress *) malloc (sizeof(ZFileProgress));
		progress->sockPair = socketPair;
		progress->zFile = zFile;
		progress->iter = gtk_tree_iter_copy(&iter);
		pthread_create(&thread, NULL, threadProgress_ZFile, progress);
		g_free(fileSave);
		}
	    gtk_widget_destroy(dialog);
	    }
	}
    (void) model;
    (void) tree_view;
    (void) column;
    }

ZFile *create_ZFile()
    {
    GtkTreeViewColumn * column;
    GtkCellRenderer *renderer;
    GtkTreeSelection *select;
    ZFile *f = (ZFile *) malloc(sizeof(ZFile));

    f->store = gtk_tree_store_new(N_COLUMNS_FIC, G_TYPE_STRING, G_TYPE_INT,
	    G_TYPE_BOOLEAN, G_TYPE_POINTER);
    f->tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(f->store));

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Fichier", renderer,
	    "text", NAME_COLUMN_FIC, NULL);
    gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
    gtk_tree_view_append_column(GTK_TREE_VIEW(f->tree), column);

    renderer = gtk_cell_renderer_progress_new();
    column = gtk_tree_view_column_new_with_attributes("Téléchargement",
	    renderer, "value", DOWN_COLUMN_FIC, NULL);
    gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
    gtk_tree_view_append_column(GTK_TREE_VIEW(f->tree), column);

    /* The column can be clicked by the user */
    gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(f->tree), TRUE);

    /* Setup the selection handler */
    select = gtk_tree_view_get_selection(GTK_TREE_VIEW(f->tree));
    gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);

    /*
     g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(
     tree_selection_changed_cb), NULL);
     */
    /* Create a scroll widget to resize the tree */
    f->box = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(f->box), f->tree);
    g_signal_connect(G_OBJECT(f->tree), "row-activated", G_CALLBACK(
	    treeSelectionActivated_ZFile), f);
    return f;
    }

GtkWidget *getContainer_ZFile(ZFile *zFile)
    {
    return zFile->box;
    }

void changeListFile_ZFile(ZFile *zFile, Client *client)
    {
    gtk_tree_store_clear(zFile->store);

    if (client != NULL)
	{
	List *list = getLFile_Client(client);
	GtkTreeIter iter;

	if (getSize_List(list) > 0)
	    {
	    first_List(list);
	    do
		{
		gtk_tree_store_append(zFile->store, &iter, NULL);
		gtk_tree_store_set(zFile->store, &iter, NAME_COLUMN_FIC,
			getDataCurrent_List(list), CLIENT_COLUMN_FIC, client,
			DOWN_COLUMN_FIC, 0, -1);
		}
	    while (next_List(list) == LIST_SUCCESS);
	    }
	}
    }
